package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.DestVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import sun.security.krb5.internal.crypto.Aes128;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 猫教授
 * @date 2019/8/10 13:31
 */
@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;
    @Reference
    private IRegionService regionService;


    @Override
    public void deleteByPrimaryKey(Long id) {

    }

    @Override
    public void insert(Destination record) {

    }

    @Override
    public Destination selectByPrimaryKey(Long id) {
        return destinationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Destination> selectAll() {
        return destinationMapper.getListAll();
    }

    @Override
    public void updateByPrimaryKey(Destination record) {

    }

    @Override
    public PageInfo<Destination> listAll(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Destination> list = destinationMapper.selectAll(qo);
        return new PageInfo<Destination>(list);
    }

    @Override
    public List<Destination> selectDestByDeep(int deep) {
        return destinationMapper.selectDestByDeep(deep);
    }

    @Override
    public Destination selecOne(Long destinationId) {
        return destinationMapper.selecOne(destinationId);
    }

    @Override
    public List<Destination> selectHotdestTplByParentId(Long... ParentIds) {
        return destinationMapper.selectHotdestTplByParentId(ParentIds);
    }

    @Override
    public List<Destination> selectHotdestTplByChinaId() {
        return destinationMapper.selectHotdestTplByChinaId();
    }

    @Override
    public Destination getCountry(Long id) {
        ArrayList<Destination> list = new ArrayList<>();
        getToasts(id,list);
        return list.get(list.size()-1);
    }

    @Override
    public Destination getProvince(Long id) {
        ArrayList<Destination> list = new ArrayList<>();
        getToasts(id,list);

        if (list.size()>1){
        return list.get(list.size()-2);
        }

        return null;
    }

    @Override
    public List<DestVO> selectCountry() {
        List<DestVO> dlist = new ArrayList<>();
        //先查询所有的区域
        List<Region> regions = regionService.selectRegions();
        for (Region region : regions) {
            DestVO destVO = new DestVO();
            List<Map<String,Object>> list = new ArrayList<>();
            int keyWord =97;
            for(int i=0;i<26;i++){
                List<Destination> countrys = destinationMapper.selectCountry((char)keyWord, region.getId());
                if(countrys.size()!=0){
                Map<String,Object> contrysMap = new HashMap<>();
                contrysMap.put("key",(char)(keyWord-32));
                contrysMap.put("countries",countrys);
                list.add(contrysMap);
                }
                keyWord+=1;
            }
            destVO.setName(region.getName());
            destVO.setDList(list);
            dlist.add(destVO);
        }
        return dlist;
    }

    /**
     * 根据当前目的地经纬度查找周边目的地
     * @param longitude  经度
     * @param latitude   纬度
     * @return
     */
    @Override
    public List<Destination> selectTude(Double longitude, Double latitude) {
        List<Destination> list = destinationMapper.selectTude(longitude, latitude);
        return list;
    }

    @Override
    public List<Destination> getDestByMonth(Long month) {
        return destinationMapper.getDestByMonth(month);
    }

    @Override
    public List<DestinationlThemeDetial> selectMonth() {
        return destinationMapper.getDetailByTheme(5L);
    }

    @Override
    public List<DestinationTheme> selectTheme() {
        return destinationMapper.selectTheme();
    }

    @Override
    public List<DestinationlThemeDetial> getDetailByTheme(Long themeId) {
        return destinationMapper.getDetailByTheme(themeId);
    }

    @Override
    public List<DestinationlThemeDetial> selectFestival() {
        return destinationMapper.getDetailByTheme(4L);
    }

    @Override
    public DestVO selectThemeAndDetial(String method) {
        DestVO destVO = new DestVO();

        List<Map<String, Object>> list = new ArrayList<>();
        List<DestinationTheme> destinationThemes = destinationMapper.selectTheme();
        for (DestinationTheme theme : destinationThemes) {
            List<DestinationlThemeDetial> detail = destinationMapper.getDetailByTheme(theme.getId());
            Map<String,Object> themeMap = new HashMap<>();
            if(!theme.getName().contains(method)){
            themeMap.put("nameKey",theme.getName());
            themeMap.put("themeKey",detail);
            list.add(themeMap);
            }
        }
        destVO.setDList(list);
        return destVO;
    }

    @Override
    public List<DestinationlThemeDetial> selectDay() {
        return destinationMapper.getDetailByTheme(6L);
    }

    @Override
    public List<Attractions> selectSpot(Long destId) {
        return destinationMapper.selectSpot(destId);
    }


    /**
     * 内部方法用来迭代吐丝的
     *
     * @param destinationId
     * @param toasts
     */
    private void getToasts(Long destinationId, ArrayList toasts) {
        Destination destination = destinationMapper.selecOne(destinationId);
        toasts.add(destination);
        if (destination.getParent() != null) {
            getToasts(destination.getParent().getId(), toasts);
        }
    }

}


